package com.ylyq.ylxx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylyq.ylxx.constants.AccountConstant;
import com.ylyq.ylxx.context.FilterContextHandler;
import com.ylyq.ylxx.entity.*;
import com.ylyq.ylxx.mapper.*;
import com.ylyq.ylxx.model.bean.*;
import com.ylyq.ylxx.model.request.CreateCoverRequest;
import com.ylyq.ylxx.model.request.RemoveCoverRequest;
import com.ylyq.ylxx.model.request.SaveYlyqCover;
import com.ylyq.ylxx.model.request.UpdateCoverRequest;
import com.ylyq.ylxx.service.IYlyqCoverCommonStyleService;
import com.ylyq.ylxx.service.IYlyqCoverElementsService;
import com.ylyq.ylxx.service.IYlyqCoverService;
import com.ylyq.ylxx.service.IYlyqCoverWechatService;
import com.ylyq.ylxx.util.HttpClientUtils;
import com.ylyq.ylxx.util.Tools;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * @author 577
 * @className YlyqCoverServiceImpl
 * @date 2020/10/29
 **/
@Service
public class YlyqCoverServiceImpl extends ServiceImpl<YlyqCoverMapper, YlyqCover> implements IYlyqCoverService {

    @Autowired
    private YlyqCoverMapper ylyqCoverMapper;
    @Autowired
    private YlyqCoverMessageMapper ylyqCoverMessageMapper;
    @Autowired
    private YlyqCoverPagesMapper ylyqCoverPagesMapper;
    @Autowired
    private YlyqCoverCommonStyleMapper ylyqCoverCommonStyleMapper;
    @Autowired
    private YlyqCoverElementsMapper ylyqCoverElementsMapper;
    @Autowired
    private YlyqCoverWechatMapper ylyqCoverWechatMapper;
    @Autowired
    private YlyqCoverPhoneMapper ylyqCoverPhoneMapper;
    @Autowired
    private IYlyqCoverElementsService iYlyqCoverElementsService;
    @Autowired
    private IYlyqCoverCommonStyleService iYlyqCoverCommonStyleService;

    /**
     * 功能描述：新增落地页
     * @author 577
     * @param
     * @return
     * @date 2020/10/30
     **/
    @Override
    public SaveYlyqCoverResponse save(SaveYlyqCover saveYlyqCover){
        YlyqCover ylyqCover = new YlyqCover();
        SaveYlyqCoverResponse saveYlyqCoverResponse = new SaveYlyqCoverResponse();
        BeanUtils.copyProperties(saveYlyqCover,ylyqCover);
        // 生成ID
        ylyqCover.setId(Tools.getUUID());
        for(GetYlyqCoverPages getYlyqCoverPages : saveYlyqCover.getPages()){
            YlyqCoverPages ylyqCoverPages = new YlyqCoverPages();
            BeanUtils.copyProperties(getYlyqCoverPages,ylyqCoverPages);

            // 生成ID
            ylyqCoverPages.setId(Tools.getUUID());
            ylyqCoverPages.setCoverId(ylyqCover.getId());
            // 添加
            ylyqCoverPagesMapper.insert(ylyqCoverPages);
            for(GetYlyqCoverPages ylyqCoverPages1:saveYlyqCover.getPages()){
                for(SaveYlyqCoverElements elements :ylyqCoverPages1.getElements()){
                    YlyqCoverElements ylyqCoverElements = new YlyqCoverElements();
                    BeanUtils.copyProperties(elements,ylyqCoverElements);
                    ylyqCoverElements.setPagesId(ylyqCoverPages.getId());
                    // 添加元素数据
                    ylyqCoverElementsMapper.insert(ylyqCoverElements);
                }
                YlyqCoverCommonStyle ylyqCoverCommonStyle = new YlyqCoverCommonStyle();
                BeanUtils.copyProperties(ylyqCoverPages1.getCommonStyle(),ylyqCoverCommonStyle);
                ylyqCoverCommonStyle.setPagesId(ylyqCoverPages.getId());
                // 添加样式数据
                ylyqCoverCommonStyleMapper.insert(ylyqCoverCommonStyle);
            }
        }

        // 添加落地页
        ylyqCoverMapper.insert(ylyqCover);
        saveYlyqCoverResponse.setId(ylyqCover.getId());
        return saveYlyqCoverResponse;

    }

    /**
     * 功能描述：删除落地页
     * @author 577
     * @param removeCoverRequest 删除落地页所需要的数据
     * @return
     * @date 2020/11/4
     **/
    @Override
    public int delete(RemoveCoverRequest removeCoverRequest){
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "tools/third_site/create/";
        // 请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());
        // 发送请求
        HttpClientUtils.post(open_api_url_prefix + uri, JSONObject.toJSON(removeCoverRequest).toString(), headers);

        QueryWrapper<YlyqCoverPages> coverPagesQueryWrapper = new QueryWrapper<>();
        coverPagesQueryWrapper.eq("cover_id",removeCoverRequest.getId());
        List<YlyqCoverPages> ylyqCoverPagesList = ylyqCoverPagesMapper.selectList(coverPagesQueryWrapper);
        for(YlyqCoverPages ylyqCoverPages : ylyqCoverPagesList){
            QueryWrapper<YlyqCoverElements> coverElementsQueryWrapper = new QueryWrapper<>();
            coverElementsQueryWrapper.eq("pages_id",ylyqCoverPages.getId());
            ylyqCoverElementsMapper.delete(coverElementsQueryWrapper);

            QueryWrapper<YlyqCoverCommonStyle> coverCommonStyleQueryWrapper = new QueryWrapper<>();
            coverCommonStyleQueryWrapper.eq("pages_id",ylyqCoverPages.getId());
            ylyqCoverCommonStyleMapper.delete(coverCommonStyleQueryWrapper);
        }
        // 删除存储元素和样式的数据
        ylyqCoverPagesMapper.delete(coverPagesQueryWrapper);
        // 删除落地页
        return ylyqCoverMapper.deleteById(removeCoverRequest.getId());
    }

    /**
     * 功能描述：删除模板落地页
     * @author 577
     * @param id 落地页ID
     * @return
     * @date 2020/11/4
     **/
    @Override
    public int deleteTemplateCover(String id){
        return ylyqCoverMapper.deleteById(id);
    }

    /**
     * 功能描述：复制落地页
     * @author 577
     * @param id 落地页ID
     * @return
     * @date 2020/10/30
     **/
    @Override
    public SaveYlyqCoverResponse copy(String id){
        SaveYlyqCoverResponse saveYlyqCoverResponse = new SaveYlyqCoverResponse();
        YlyqCover ylyqCover = ylyqCoverMapper.selectById(id);
        QueryWrapper<YlyqCoverPages> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cover_id",ylyqCover.getId());
        List<YlyqCoverPages> ylyqCoverPagesList = ylyqCoverPagesMapper.selectList(queryWrapper);
        ylyqCover.setId(Tools.getUUID());
        ylyqCover.setIsTemplate(false);
        for(YlyqCoverPages ylyqCoverPages : ylyqCoverPagesList){
            ylyqCoverPages.setId(Tools.getUUID());
            ylyqCoverPages.setCoverId(ylyqCover.getId());
            // 查询落地页的元素
            QueryWrapper<YlyqCoverElements> queryWrapper2 = new QueryWrapper<>();
            queryWrapper.eq("pages_id",ylyqCoverPages.getId());
            List<YlyqCoverElements> ylyqCoverElementList = ylyqCoverElementsMapper.selectList(queryWrapper2);
            for(YlyqCoverElements ylyqCoverElements : ylyqCoverElementList){
                ylyqCoverElements.setId(Tools.getUUID());
                ylyqCoverElements.setPagesId(ylyqCoverPages.getId());
                ylyqCoverElementsMapper.insert(ylyqCoverElements);
            }
            // 查询落地页的样式
            QueryWrapper<YlyqCoverCommonStyle> queryWrapper3 = new QueryWrapper<>();
            queryWrapper.eq("pages_id",ylyqCoverPages.getId());
            List<YlyqCoverCommonStyle> ylyqCoverCommonStyles = ylyqCoverCommonStyleMapper.selectList(queryWrapper3);
            for(YlyqCoverCommonStyle ylyqCoverCommonStyle : ylyqCoverCommonStyles){
                ylyqCoverCommonStyle.setId(Tools.getUUID());
                ylyqCoverCommonStyle.setPagesId(ylyqCoverPages.getId());
                ylyqCoverCommonStyleMapper.insert(ylyqCoverCommonStyle);
            }
            ylyqCoverPagesMapper.insert(ylyqCoverPages);
        }
        ylyqCoverMapper.insert(ylyqCover);
        saveYlyqCoverResponse.setId(ylyqCover.getId());
        return saveYlyqCoverResponse;
    }

    /**
     * 功能描述：获取落地页列表
     * @author 577
     * @param pageMode 落地页类型
     * @return 
     * @date 2020/10/30
     **/
    @Override
    public List<GetYlyqCover> getCover(String pageMode){
        QueryWrapper<YlyqCover> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pageMode",pageMode);
        queryWrapper.eq("isTemplate",false);
        queryWrapper.eq("del_flag",0);
        List<GetYlyqCover> getYlyqCovers = new ArrayList<>();
        List<YlyqCover> ylyqCovers = list(queryWrapper);
        for(YlyqCover ylyqCover : ylyqCovers){
            GetYlyqCover getYlyqCover = new GetYlyqCover();
            BeanUtils.copyProperties(ylyqCover,getYlyqCover);
            getYlyqCover.setId(ylyqCover.getId());
            getYlyqCovers.add(getYlyqCover);
            // 根据落地页ID查询落地页元素与样式
            QueryWrapper<YlyqCoverPages> queryWrapper2 = new QueryWrapper<>();
            queryWrapper.eq("coverId",getYlyqCover.getId());
            List<YlyqCoverPages> ylyqCoverPages = ylyqCoverPagesMapper.selectList(queryWrapper2);
            List<GetYlyqCoverPages> ylyqCoverPagesList = new ArrayList<>();
            for(YlyqCoverPages y : ylyqCoverPages){
                GetYlyqCoverPages getYlyqCoverPages = new GetYlyqCoverPages();
                BeanUtils.copyProperties(y,getYlyqCoverPages);
                // 元素集合
                QueryWrapper<YlyqCoverElements> queryWrapper3 = new QueryWrapper<>();
                queryWrapper.eq("pagesId",y.getId());
                List<YlyqCoverElements> ylyqCoverElements = ylyqCoverElementsMapper.selectList(queryWrapper3);
                List<SaveYlyqCoverElements> saveYlyqCoverElements = new ArrayList<>();
                for(YlyqCoverElements y1 : ylyqCoverElements){
                    SaveYlyqCoverElements saveYlyqCoverElements1 = new SaveYlyqCoverElements();
                    BeanUtils.copyProperties(y1,saveYlyqCoverElements1);
                    saveYlyqCoverElements.add(saveYlyqCoverElements1);
                }
                // 样式集合
                QueryWrapper<YlyqCoverCommonStyle> queryWrapper4 = new QueryWrapper<>();
                queryWrapper.eq("pagesId",y.getId());
                List<YlyqCoverCommonStyle> ylyqCoverCommonStyles = ylyqCoverCommonStyleMapper.selectList(queryWrapper4);
                SaveYlyqCoverCommonStyle saveYlyqCoverCommonStyle = new SaveYlyqCoverCommonStyle();
                BeanUtils.copyProperties(ylyqCoverCommonStyles.get(0),saveYlyqCoverCommonStyle);
                // 将元素与样式的数据存进集合中
                getYlyqCoverPages.setCommonStyle(saveYlyqCoverCommonStyle);
                getYlyqCoverPages.setElements(saveYlyqCoverElements);
                ylyqCoverPagesList.add(getYlyqCoverPages);
            }
            getYlyqCover.setPages(ylyqCoverPagesList);
        }
        return getYlyqCovers;
    }

    /**
     * 功能描述：获取模版落地页列表
     * @author 577
     * @param
     * @return
     * @date 2020/11/4
     **/
    @Override
    public List<GetYlyqCover> getTemplateCover(){
        QueryWrapper<YlyqCover> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isTemplate",true);
        List<GetYlyqCover> getYlyqCovers = new ArrayList<>();
        List<YlyqCover> ylyqCovers = list(queryWrapper);
        for(YlyqCover ylyqCover : ylyqCovers){

            // 查询微信号
            QueryWrapper<YlyqCoverWechat> wechatQueryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cover_id",ylyqCover.getId());
            List<YlyqCoverWechat> ylyqCoverWechats = ylyqCoverWechatMapper.selectList(wechatQueryWrapper);
            List<GetCoverWechatResponse> coverWechats = new ArrayList<>();
            for(YlyqCoverWechat ylyqCoverWechat : ylyqCoverWechats ){
                GetCoverWechatResponse getCoverWechatResponse = new GetCoverWechatResponse();
                BeanUtils.copyProperties(ylyqCoverWechat,getCoverWechatResponse);
                coverWechats.add(getCoverWechatResponse);
            }

            // 查询手机号
            QueryWrapper<YlyqCoverPhone> phoneQueryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cover_id",ylyqCover.getId());
            List<YlyqCoverPhone> ylyqCoverPhones = ylyqCoverPhoneMapper.selectList(phoneQueryWrapper);
            List<GetCoverPhoneResponse> coverPhones = new ArrayList<>();
            for(YlyqCoverPhone ylyqCoverPhone : ylyqCoverPhones ){
                GetCoverPhoneResponse getCoverPhoneResponse = new GetCoverPhoneResponse();
                BeanUtils.copyProperties(ylyqCoverPhone,getCoverPhoneResponse);
                coverPhones.add(getCoverPhoneResponse);
            }

            GetYlyqCover getYlyqCover = new GetYlyqCover();
            BeanUtils.copyProperties(ylyqCover,getYlyqCover);
            getYlyqCover.setId(ylyqCover.getId());
            getYlyqCovers.add(getYlyqCover);
            // 根据落地页ID查询落地页元素与样式
            QueryWrapper<YlyqCoverPages> queryWrapper2 = new QueryWrapper<>();
            queryWrapper.eq("coverId",getYlyqCover.getId());
            List<YlyqCoverPages> ylyqCoverPages = ylyqCoverPagesMapper.selectList(queryWrapper2);
            List<GetYlyqCoverPages> ylyqCoverPagesList = new ArrayList<>();
            for(YlyqCoverPages y : ylyqCoverPages){
                GetYlyqCoverPages getYlyqCoverPages = new GetYlyqCoverPages();
                BeanUtils.copyProperties(y,getYlyqCoverPages);
                // 元素集合
                QueryWrapper<YlyqCoverElements> queryWrapper3 = new QueryWrapper<>();
                queryWrapper.eq("pagesId",y.getId());
                List<YlyqCoverElements> ylyqCoverElements = ylyqCoverElementsMapper.selectList(queryWrapper3);
                List<SaveYlyqCoverElements> saveYlyqCoverElements = new ArrayList<>();
                for(YlyqCoverElements y1 : ylyqCoverElements){
                    SaveYlyqCoverElements saveYlyqCoverElements1 = new SaveYlyqCoverElements();
                    BeanUtils.copyProperties(y1,saveYlyqCoverElements1);
                    saveYlyqCoverElements.add(saveYlyqCoverElements1);
                }
                // 样式集合
                QueryWrapper<YlyqCoverCommonStyle> queryWrapper4 = new QueryWrapper<>();
                queryWrapper.eq("pagesId",y.getId());
                YlyqCoverCommonStyle ylyqCoverCommonStyles = ylyqCoverCommonStyleMapper.selectOne(queryWrapper4);
                SaveYlyqCoverCommonStyle saveYlyqCoverCommonStyle = new SaveYlyqCoverCommonStyle();
                BeanUtils.copyProperties(ylyqCoverCommonStyles,saveYlyqCoverCommonStyle);
                // 将元素与样式的数据存进集合中
                getYlyqCoverPages.setCommonStyle(saveYlyqCoverCommonStyle);
                getYlyqCoverPages.setElements(saveYlyqCoverElements);
                ylyqCoverPagesList.add(getYlyqCoverPages);
            }
            getYlyqCover.setPages(ylyqCoverPagesList);
            getYlyqCover.setCoverWechats(coverWechats);
            getYlyqCover.setCoverPhones(coverPhones);
        }
        return getYlyqCovers;
    }

    /**
     * 功能描述：获取落地页详情
     * @author 577
     * @param id 落地页详情
     * @return 
     * @date 2020/10/30
     **/
    @Override
    public GetYlyqCover getCoverDetails(String id){
        GetYlyqCover ylyqCover = new GetYlyqCover();
        BeanUtils.copyProperties(ylyqCoverMapper.selectById(id),ylyqCover);
        QueryWrapper<YlyqCoverPages> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cover_id",id);
        List<YlyqCoverPages> ylyqCoverPagesList = ylyqCoverPagesMapper.selectList(queryWrapper);
        List<GetYlyqCoverPages> getYlyqCoverPagesList = new ArrayList<>();

        // 查询微信号
        QueryWrapper<YlyqCoverWechat> wechatQueryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cover_id",ylyqCover.getId());
        List<YlyqCoverWechat> ylyqCoverWechats = ylyqCoverWechatMapper.selectList(wechatQueryWrapper);
        List<GetCoverWechatResponse> coverWechats = new ArrayList<>();
        for(YlyqCoverWechat ylyqCoverWechat : ylyqCoverWechats ){
            GetCoverWechatResponse getCoverWechatResponse = new GetCoverWechatResponse();
            BeanUtils.copyProperties(ylyqCoverWechat,getCoverWechatResponse);
            coverWechats.add(getCoverWechatResponse);
        }

        // 查询手机号
        QueryWrapper<YlyqCoverPhone> phoneQueryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cover_id",ylyqCover.getId());
        List<YlyqCoverPhone> ylyqCoverPhones = ylyqCoverPhoneMapper.selectList(phoneQueryWrapper);
        List<GetCoverPhoneResponse> coverPhones = new ArrayList<>();
        for(YlyqCoverPhone ylyqCoverPhone : ylyqCoverPhones ){
            GetCoverPhoneResponse getCoverPhoneResponse = new GetCoverPhoneResponse();
            BeanUtils.copyProperties(ylyqCoverPhone,getCoverPhoneResponse);
            coverPhones.add(getCoverPhoneResponse);
        }

        for(YlyqCoverPages ylyqCoverPages : ylyqCoverPagesList){
            GetYlyqCoverPages getYlyqCoverPages = new GetYlyqCoverPages();
            BeanUtils.copyProperties(ylyqCoverPages,getYlyqCoverPages);

            // 查询落地页元素
            QueryWrapper<YlyqCoverElements> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("pages_id",getYlyqCoverPages.getId());
            List<YlyqCoverElements> ylyqCoverElements = ylyqCoverElementsMapper.selectList(queryWrapper1);
            List<SaveYlyqCoverElements> saveYlyqCoverElementsList = new ArrayList<>();
            for(YlyqCoverElements ylyqCoverElements1 : ylyqCoverElements){
                SaveYlyqCoverElements saveYlyqCoverElements = new SaveYlyqCoverElements();
                BeanUtils.copyProperties(ylyqCoverElements1,saveYlyqCoverElements);
                saveYlyqCoverElementsList.add(saveYlyqCoverElements);
            }

            // 查询落地页样式
            QueryWrapper<YlyqCoverCommonStyle> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("pages_id",getYlyqCoverPages.getId());
            List<YlyqCoverCommonStyle> ylyqCoverCommonStyles = ylyqCoverCommonStyleMapper.selectList(queryWrapper2);
            if(ylyqCoverCommonStyles != null && ylyqCoverCommonStyles.size() > 0){
                SaveYlyqCoverCommonStyle saveYlyqCoverCommonStyle = new SaveYlyqCoverCommonStyle();
                BeanUtils.copyProperties(ylyqCoverCommonStyles.get(0),saveYlyqCoverCommonStyle);
                // 将落地页的元素与样式存入集合中
                getYlyqCoverPages.setCommonStyle(saveYlyqCoverCommonStyle);
            }
            getYlyqCoverPages.setElements(saveYlyqCoverElementsList);
            getYlyqCoverPagesList.add(getYlyqCoverPages);
        }

        ylyqCover.setPages(getYlyqCoverPagesList);
        ylyqCover.setCoverWechats(coverWechats);
        ylyqCover.setCoverPhones(coverPhones);

        return ylyqCover;
    }

    /**
     * 功能描述：使用模板落地页
     * @author 577
     * @param id 落地页ID
     * @return
     * @date 2020/10/30
     **/
    @Override
    public SaveYlyqCoverResponse useTemplateCover(String id){
        YlyqCover ylyqCover = ylyqCoverMapper.selectById(id);
        ylyqCover.setId(Tools.getUUID());
        ylyqCover.setIsTemplate(false);
        ylyqCoverMapper.insert(ylyqCover);
        SaveYlyqCoverResponse saveYlyqCoverResponse = new SaveYlyqCoverResponse();
        saveYlyqCoverResponse.setId(ylyqCover.getId());
        return saveYlyqCoverResponse;
    }

    /**
     * 功能描述：创建第三方落地页站点
     * @author 577
     * @param createCoverRequest 创建第三方落地页站点的请求参数
     * @return 
     * @date 2020/10/30
     **/
    @Override
    public CreateCoverResponse publishCover(CreateCoverRequest createCoverRequest){
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "tools/third_site/create/";
        // 请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());
        // 发送请求
        String result = HttpClientUtils.post(open_api_url_prefix + uri, JSONObject.toJSON(createCoverRequest).toString(), headers);
        JSONObject jsonObject = JSONObject.parseObject(result);
        CreateCoverResponse createCoverResponse = JSONObject.toJavaObject(jsonObject, CreateCoverResponse.class);
        YlyqCover ylyqCover = ylyqCoverMapper.selectById(createCoverRequest.getId());
        // 设置站点ID
        ylyqCover.setSiteId(createCoverResponse.getData().getSite_id());
        // 修改落地页状态
        ylyqCover.setIsPublish(true);
        ylyqCoverMapper.updateById(ylyqCover);
        return createCoverResponse;
    }

    /**
     * 功能描述：获取落地页关键指标数据
     * @author 577
     * @param
     * @return
     * @date 2020/10/30
     **/
    @Override
    public GetCountResponse getCount(String id){
        GetCountResponse getCountResponse = new GetCountResponse();
        QueryWrapper<YlyqCoverMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cover_id",id);
        getCountResponse.setSubmitCount(ylyqCoverMessageMapper.selectCount(queryWrapper));
        YlyqCover ylyqCover = ylyqCoverMapper.selectById(id);
        getCountResponse.setVisitCount(ylyqCover.getVisitCount());
        return getCountResponse;
    }

    /**
     * 功能描述：记录落地页关键指标数据
     * @author 577
     * @param id 落地页ID
     * @return
     * @date 2020/10/30
     **/
    @Override
    public int updateVisitCount(String id){
        YlyqCover ylyqCover = ylyqCoverMapper.selectById(id);
        ylyqCover.setVisitCount(+1);
        return ylyqCoverMapper.updateById(ylyqCover);
    }

    /**
     * 功能描述：落地页设置为模板落地页
     * @author 577
     * @param id 落地页ID
     * @return 
     * @date 2020/10/30
     **/
    @Override
    public SaveYlyqCoverResponse updateCover(String id){
        SaveYlyqCoverResponse saveYlyqCoverResponse = new SaveYlyqCoverResponse();
        YlyqCover ylyqCover = ylyqCoverMapper.selectById(id);
        QueryWrapper<YlyqCoverPages> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cover_id",ylyqCover.getId());
        List<YlyqCoverPages> ylyqCoverPagesList = ylyqCoverPagesMapper.selectList(queryWrapper);
        ylyqCover.setId(Tools.getUUID());
        ylyqCover.setIsTemplate(true);
        for(YlyqCoverPages ylyqCoverPages : ylyqCoverPagesList){
            ylyqCoverPages.setId(Tools.getUUID());
            ylyqCoverPages.setCoverId(ylyqCover.getId());
            // 查询落地页的元素
            QueryWrapper<YlyqCoverElements> queryWrapper2 = new QueryWrapper<>();
            queryWrapper.eq("pages_id",ylyqCoverPages.getId());
            List<YlyqCoverElements> ylyqCoverElementList = ylyqCoverElementsMapper.selectList(queryWrapper2);
            for(YlyqCoverElements ylyqCoverElements : ylyqCoverElementList){
                ylyqCoverElements.setId(Tools.getUUID());
                ylyqCoverElements.setPagesId(ylyqCoverPages.getId());
                ylyqCoverElementsMapper.insert(ylyqCoverElements);
            }
            // 查询落地页的样式
            QueryWrapper<YlyqCoverCommonStyle> queryWrapper3 = new QueryWrapper<>();
            queryWrapper.eq("pages_id",ylyqCoverPages.getId());
            List<YlyqCoverCommonStyle> ylyqCoverCommonStyles = ylyqCoverCommonStyleMapper.selectList(queryWrapper3);
            for(YlyqCoverCommonStyle ylyqCoverCommonStyle : ylyqCoverCommonStyles){
                ylyqCoverCommonStyle.setId(Tools.getUUID());
                ylyqCoverCommonStyle.setPagesId(ylyqCoverPages.getId());
                ylyqCoverCommonStyleMapper.insert(ylyqCoverCommonStyle);
            }
            ylyqCoverPagesMapper.insert(ylyqCoverPages);
        }
        ylyqCoverMapper.insert(ylyqCover);
        saveYlyqCoverResponse.setId(ylyqCover.getId());
        return saveYlyqCoverResponse;
    }

    /**
     * 功能描述：更新落地页
     * @author 577
     * @param
     * @return
     * @date 2020/10/30
     **/
    @Override
    public int updateById(UpdateCoverRequest updateCoverRequest){
        for(GetYlyqCoverPages ylyqCoverPages:updateCoverRequest.getPages()){
            QueryWrapper<YlyqCoverElements> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pages_id",ylyqCoverPages.getId());
            // 删除元素数据
            ylyqCoverElementsMapper.delete(queryWrapper);
        }

        YlyqCover ylyqCover = new YlyqCover();
        BeanUtils.copyProperties(updateCoverRequest,ylyqCover);
        for(GetYlyqCoverPages ylyqCoverPages:updateCoverRequest.getPages()){
            for(SaveYlyqCoverElements elements :ylyqCoverPages.getElements()){
                YlyqCoverElements ylyqCoverElements = new YlyqCoverElements();
                BeanUtils.copyProperties(elements,ylyqCoverElements);
                // 判断是否是要增加数据
                if(ylyqCoverElements.getId() == null && ylyqCoverElements.getPagesId() == null){
                    ylyqCoverElements.setPagesId(ylyqCoverPages.getId());
                }
                // 修改元素数据
                iYlyqCoverElementsService.saveOrUpdate(ylyqCoverElements);
            }
            YlyqCoverCommonStyle ylyqCoverCommonStyle = new YlyqCoverCommonStyle();
            if(ylyqCoverPages.getCommonStyle() != null){
                BeanUtils.copyProperties(ylyqCoverPages.getCommonStyle(),ylyqCoverCommonStyle);
                if(ylyqCoverCommonStyle.getId() == null && ylyqCoverCommonStyle.getPagesId() == null){
                    ylyqCoverCommonStyle.setPagesId(ylyqCoverPages.getId());
                }
                // 修改样式数据
                iYlyqCoverCommonStyleService.saveOrUpdate(ylyqCoverCommonStyle);
            }
        }
        // 修改落地页
        return ylyqCoverMapper.updateById(ylyqCover);
    }
}
